package cn.kickegg.techx.service.mq;

import cn.kickegg.techx.common.utils.id.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author chenkai
 * @createTime 2024/6/6 16:38
 * @description
 * @keyPoint
 */
@Slf4j
@RestController
public class RocketmqProducerController {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    // 延时消息，RocketMQ支持这几个级别的延时消息，自定义需要修改broker配置文件
    // 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
    @GetMapping("/rocket/delayMsg/send")
    public String rocketDelayMsgSend() {
        LocalDateTime currentDateTime = LocalDateTime.now();
        rocketMQTemplate.syncSend("rocket-topic-2:tag-2", MessageBuilder.withPayload(currentDateTime.toString()).build(), 2000, 3);
        return currentDateTime.toString();
    }

    @GetMapping("/rocket/send")
    public String rocketMsgSend() {
        LocalDateTime currentDateTime = LocalDateTime.now();

        String key = IdUtils.getSnowflakeId().toString();
        String msg = key + "-" + currentDateTime.toString();

        rocketMQTemplate.send("rocket-topic-2",
                MessageBuilder.withPayload(msg)
                        .setHeader("KEYS", key)
                        .build());

        log.info("send msg: {}", msg);
        return currentDateTime.toString();
    }

}
